import { baseURL } from './../config/index'
import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './route'

Vue.use(VueRouter)

export class Router {
  /**
   * 保存实例
   *
   * @private
   * @static
   * @type {VueRouter}
   * @memberOf Router
   */
  private static router: VueRouter | null = null

  /**
   * 创建路由
   *
   * @static
   * @param {string} [activeRule=baseURL]
   * @returns
   *
   * @memberOf Router
   */
  public static create(activeRule = baseURL) {
    if (this.router) {
      return this.router
    }

    const router = (this.router = new VueRouter({
      mode: 'hash',
      base: activeRule,
      routes: routes,
    }))

    router.afterEach(route => {
      window.scrollTo(0, 0)
    })

    return router
  }

  /**
   * 销毁
   *
   * @static
   *
   * @memberOf Router
   */
  public static destroy() {
    this.router = null
  }
}
